package com.hixtrip.sample.infra;

import com.hixtrip.sample.domain.order.model.Order;
import com.hixtrip.sample.domain.order.repository.OrderRepository;
import com.hixtrip.sample.domain.pay.model.CommandPay;
import com.hixtrip.sample.infra.db.convertor.OrderDOConvertor;
import com.hixtrip.sample.infra.db.dataobject.OrderEntity;
import com.hixtrip.sample.infra.db.mapper.OrderMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
public class OrderRepositoryImpl implements OrderRepository {
    @Resource
    private OrderMapper orderMapper;

    @Override
    public int createOrder(Order order) {
//        OrderEntity orderEntity = OrderDOConvertor.INSTANCE.orderEntityBuild(order);
        OrderEntity orderEntity = OrderDOConvertor.orderEntityBuild(order);
        return orderMapper.createOrder(orderEntity);

    }

    @Override
    public void payOk(CommandPay commandPay) {
        String orderId = commandPay.getOrderId();
        //取余 基因法
        Long tableIndex = Long.parseLong(orderId) % 4;
        String tableName = "t_order_".concat(tableIndex.toString());
        orderMapper.updateOrderStatus(Long.valueOf(commandPay.getOrderId()),tableName,new Date());


    }

    @Override
    public void payError(CommandPay commandPay) {
        System.out.println("支付失败");

    }
}
